<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .banner {
            width: 1200px;
            height: 500px;
            background-color: pink;
            position: relative;
            margin: 0 auto;
        }

        .imgs {
            width: 100%;
            height: 100%;
            background-color: antiquewhite;
        }

        .imgs li {
            width: 1200px;
            height: 500px;
            position: absolute;
            left: 0;
            top: 0;
            font-size: 50px;
            text-align: center;
            line-height: 500px;
            color: #fff;
            transition: all 1s ease-in;
            opacity: 0.1;
        }

        .imgs li:nth-child(1) {
            background-color: aqua;
            z-index: 1;
        }

        .imgs li:nth-child(2) {
            background-color: blue;
        }

        .imgs li:nth-child(3) {
            background-color: chartreuse;
        }

        .imgs li:nth-child(4) {
            background-color: darkcyan;
        }

        li.active {
            opacity: 1;
        }

        .btns {
            width: 200px;
            height: 30px;
            position: absolute;
            left: 500px;
            bottom: 20px;
            z-index: 999;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .btns li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #ccc;
        }

        li.btn_active {
            background-color: #fff;
        }

        .prev,
        .next {
            width: 60px;
            height: 80px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 210px;
            z-index: 999;
        }

        .prev {
            left: 0;
        }

        .next {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="banner">
        <ul class="imgs">
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <ul class="btns">
            <li class="btn_active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="prev"></div>
        <div class="next"></div>
    </div>
    <script>
        const btns = document.querySelectorAll(".btns li");
        const imgs = document.querySelectorAll(".imgs li");
        const banner = document.querySelector(".banner");
        const prev = document.querySelector(".prev");
        const next = document.querySelector(".next");
        let nowActiveImg = imgs[0];
        let nowActiveBtn = btns[0];
        let z = 2;
        btns.forEach((btn, i) => {
            btn.onclick = () => {
                now = i;
                nowActiveImg.classList.remove("active");
                imgs[i].classList.add("active");
                nowActiveImg = imgs[i];
                nowActiveImg.style.zIndex = z++;

                nowActiveBtn.classList.remove("btn_active");
                btn.classList.add("btn_active");
                nowActiveBtn = btn;
            }
        });
        let now = 0;
        const swap = () => {
            now++;
            if (now === -1) {
                now = 3;
            }
            now %= 4;
            nowActiveImg.classList.remove("active");
            imgs[now].classList.add("active");
            nowActiveImg = imgs[now];
            nowActiveImg.style.zIndex = z++;

            nowActiveBtn.classList.remove("btn_active");
            btns[now].classList.add("btn_active");
            nowActiveBtn = btns[now];
        }
        let t = setInterval(swap, 3000);
        banner.onmouseenter = () => {
            clearInterval(t);
        }
        banner.onmouseleave = () => {
            t = setInterval(swap, 3000);
        }
        prev.onclick = throttle2(() => {
            now -= 2;
            swap();
        }, 1000);
        next.onclick = throttle1(() => {
            swap();
        }, 1000);
        //节流throttle
        function throttle1(cb, time) {
            let lastTime = 0;
            return function () {
                let nowTime = Date.now();
                if (nowTime - lastTime >= time) {
                    cb();
                    lastTime = nowTime;
                }
            }
        }
        function throttle2(cb, time) {
            let t;
            return () => {
                if (t) {
                    return;
                }
                t = setTimeout(() => {
                    cb();
                    t = null;
                }, time)
            }
        }
        //防抖debounce 
        // function debounce(cb,time){
        //     let timer;
        //     return function(){
        //         clearTimeout(timer);
        //         timer=setTimeout(()=>{
        //             cb();
        //         },time);
        //     }
        // }
    </script>
</body>

</html>